<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Campaign_model extends CI_Model {

    function add_campaign($data_save) {
        $data_save['created_at'] = time();
        $this->db->insert('campaigns', $data_save);
        return $this->db->insert_id();
    }

    function edit_campaign($campaign_id, $data_save) {

        $this->db->where('id', $campaign_id)->update('campaigns', $data_save);
        return $this->db->insert_id();
    }

    function list_campaign() {
        $query = $this->db->get('campaigns');
        if ($query->num_rows() > 0) {
            return $query->result_array();
        }
        return array();
    }

    /*
      function list_battle_campaign() {
      $query = $this->db->select('hash,campaigns.title,sponsors.title as sponsor_title,sponsors.css_class,sponsors.css_preset,sponsors.logo,candidate_vdo,candidate_image,candidate_text,positive_score_reward,view_score_reward,wilson_score_reward,created_at,starting_at')->join('sponsors', 'sponsors.id=campaigns.sponsor_id')->where('status', 'starting')->where('is_private !=', 1)->order_by('waiting_at', 'DESC')->get('campaigns');
      if ($query->num_rows() > 0) {
      return $query->result_array();
      }
      return array();
      }
     * 
     */

    function list_recent_campaign() {
        $this->load->driver('cache', array('adapter' => 'file'));
        if (!$foo = $this->cache->get('list_recent_campaign')) {
            $query = $this->db->select('campaigns.id,status,campaigns.title,sponsors.title as sponsor_title,sponsors.css_class,sponsors.css_preset,sponsors.logo,candidate_vdo,candidate_image,candidate_text,positive_score_reward,view_score_reward,wilson_score_reward,created_at,starting_at')->join('sponsors', 'sponsors.id=campaigns.sponsor_id')->where('status !=', 'pending')->where('is_private !=', 1)->order_by('created_at', 'DESC')->get('campaigns');
            if ($query->num_rows() > 0) {
                $foo = $query->result_array();
            } else {
                $foo = array();
            }
            $this->cache->save('list_recent_campaign', $foo, 300);
        }
        return $foo;
    }

    function list_coming_campaign() {
        $this->load->driver('cache', array('adapter' => 'file'));
        if (!$foo = $this->cache->get('list_coming_campaign')) {
            $query = $this->db->select('campaigns.id,status,campaigns.title,sponsors.title as sponsor_title,sponsors.css_class,sponsors.css_preset,sponsors.logo,candidate_vdo,candidate_image,candidate_text,positive_score_reward,view_score_reward,wilson_score_reward,created_at,starting_at')
                    ->join('sponsors', 'sponsors.id=campaigns.sponsor_id')
                    ->where('status', 'pending')
                    ->where('is_private !=', 1)
                    ->order_by('created_at', 'DESC')->get('campaigns');
            if ($query->num_rows() > 0) {
                $foo = $query->result_array();
            } else {
                $foo = array();
            }
            $this->cache->save('list_coming_campaign', $foo, 300);
        }
        return $foo;
    }

    function list_announce_campaign() {
        $query = $this->db->select('campaigns.title,sponsors.title as sponsor_title,sponsors.css_class,sponsors.css_preset,sponsors.logo,candidate_vdo,candidate_image,candidate_text,positive_score_reward,view_score_reward,wilson_score_reward,created_at,starting_at')->join('sponsors', 'sponsors.id=campaigns.sponsor_id')->where('status', 'stopped')->where('is_private !=', 1)->order_by('stopped_at', 'DESC')->get('campaigns');
        if ($query->num_rows() > 0) {
            return $query->result_array();
        }
        return array();
    }

    function list_campaign_get_sponsor_logo() {
        $query = $this->db->select('campaigns.*,sponsors.logo')->join('sponsors', 'sponsors.id=campaigns.sponsor_id')->order_by('created_at', 'DESC')->get('campaigns');
        if ($query->num_rows() > 0) {
            return $query->result_array();
        }
        return array();
    }

    function get_campaign($campaign_id) {
        $row = $this->db->where('id', $campaign_id)->get('campaigns')->row_array();
        return $row ? $row : array();
    }

    function get_campaign_by_hash($hash) {

        $campaign_id = hashids_decrypt($hash);
        
        if ($campaign_id) {
            //$this->load->driver('cache', array('adapter' => 'file'));
            //if (!$foo = $this->cache->get('get_campaign_by_hash_' . $hash)) {
            $foo = $this->db->select('sponsor_id,highlight,highlight_interval,campaigns.id,status,description,campaigns.title,sponsors.title as sponsor_title,sponsors.css_class,sponsors.css_preset,sponsors.logo,candidate_vdo,candidate_image,candidate_text,positive_score_reward,view_score_reward,wilson_score_reward,candidate_amount,created_at,starting_at,stopped_at')->join('sponsors', 'sponsors.id=campaigns.sponsor_id')->where('campaigns.id', $campaign_id)->get('campaigns')->row_array();
            // Save into the cache for 10 minutes
            //$this->cache->save('get_campaign_by_hash_' . $hash, $foo, 30);
            //}

            return $foo ? $foo : array();
        }
        return array();
    }

    function delete_campaign($campaign_id) {
        return $this->db->where('id', $campaign_id)->delete('campaigns');
    }

    private function format_countdown($expire_at) {
        $remaining = $expire_at - time();
        $days_remaining = floor($remaining / 86400);
        $hours_remaining = floor(($remaining % 86400) / 3600);
        return $days_remaining . ' วัน ' . $hours_remaining . ' ชั่วโมง';
    }

    function status_title_campaign_icon($status) {
        switch ($status) {
            case 'pending': return '<span class="label label-important">ยังไม่เริ่มการแข่งขัน</span>';
            case 'starting': return '<span class="label label-success">กำลังแข่งขัน</span>';
            case 'stopped': return '<span class="label label-inverse">จบการแข่งขันแล้ว</span>';
        }
    }

    function status_title_campaign($status, $change_timestamp) {
        switch ($status) {
            case 'pending': return '<span class="label label-important">ยังไม่เริ่มการแข่งขัน</span><br/><hr/>เปลี่ยนสถานะภายใน: ' . $this->format_countdown($change_timestamp['starting_at']);
            case 'starting': return '<span class="label label-success">กำลังแข่งขัน</span><hr/>เปลี่ยนสถานะภายใน: ' . $this->format_countdown($change_timestamp['stopped_at']);
            case 'stopped': return '<span class="label label-inverse">จบการแข่งขันแล้ว</span>';
        }
    }

    private function format_countdown_detail($expire_at) {
        $remaining = $expire_at - time();
        $days_remaining = floor($remaining / 86400);
        $hours_remaining = floor(($remaining % 86400) / 3600);
        $mins_remaining = floor(($remaining % 3600) / 60);
        $return = '';
        if ($days_remaining > 0)
            $return .= $days_remaining . ' วัน ';

        if ($hours_remaining > 0)
            $return .= $hours_remaining . ' ชั่วโมง ';

        if ($mins_remaining > 0)
            $return .= $mins_remaining . '  นาที';

        return $return;
    }

    function get_remain_day($expire_at) {
        return $this->format_countdown_detail($expire_at);
    }

    function campaign_reward_title_display($reward_data) {

        $result = '';
        foreach ($reward_data as $name => $reward) {
           $reward_array = explode('|', $reward);
            if ($reward_array[0] == 0) {

                continue;
            }

            switch ($name) {
                case 'positive_score_reward':$result .= '<i class="icon-thumbs-up"></i> โหวตชอบมากที่สุด ( ' . $reward . ' อันดับ )<br/>';
                    break;
                case 'view_score_reward':$result .= '<i class="icon-eye-open"></i> ผู้ชมมากที่สุด ( ' . $reward . ' อันดับ )<br/>';
                    break;
                case 'wilson_score_reward':$result .= '<i class="icon-bolt"></i> โหวตรวมมากที่สุด ( ' . $reward . ' อันดับ )<br/>';
                    break;
            }
        }
        if ($result != '') {
            echo '<hr/>';
        }
        return $result;
    }

}